package neusoft.example.com.yuejian06.Activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

import neusoft.example.com.yuejian06.Fragment.ClubListFragment;
import neusoft.example.com.yuejian06.Fragment.HomeFragment;
import neusoft.example.com.yuejian06.Fragment.MyFragment;
import neusoft.example.com.yuejian06.Fragment.PictureListFragment;
import neusoft.example.com.yuejian06.Fragment.TrainListFragment;
import neusoft.example.com.yuejian06.R;

public class MainActivity extends AppCompatActivity {
    private ImageView image_home,image_train,image_club,image_picture,image_my;
    private LinearLayout home,train,club,picture,my;
    private ViewPager viewPager;
    private List<Fragment> listfragment;
    private FragmentPagerAdapter fpadapter;
    private int select = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null){
            actionBar.setDisplayHomeAsUpEnabled(true);
            //actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
        }
        select = getIntent().getIntExtra("selectTab",0);
        initView();
        initEvents();
        initFragment();
        selectTab(select);
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            resetImage();
            switch (view.getId()) {
                case R.id.bottom_home:
                    selectTab(0);
                    break;
                case R.id.bottom_train:
                    selectTab(1);
                    break;
                case R.id.bottom_club:
                    selectTab(2);
                    break;
                case R.id.bottom_picture:
                    selectTab(3);
                    break;
                case R.id.bottom_my:
                    selectTab(4);
                    break;
            }
        }
    };



    private void initView(){
        image_home = (ImageView) findViewById(R.id.image_home);
        image_train = (ImageView) findViewById(R.id.image_train);
        image_club = (ImageView) findViewById(R.id.image_club);
        image_picture = (ImageView) findViewById(R.id.image_picture);
        image_my = (ImageView) findViewById(R.id.image_my);

        home = (LinearLayout) findViewById(R.id.bottom_home);
        train = (LinearLayout) findViewById(R.id.bottom_train);
        club = (LinearLayout) findViewById(R.id.bottom_club);
        picture = (LinearLayout) findViewById(R.id.bottom_picture);
        my = (LinearLayout) findViewById(R.id.bottom_my);

        viewPager = (ViewPager) findViewById(R.id.viewPager);
    }
    private void initEvents(){
        home.setOnClickListener(onClickListener);
        train.setOnClickListener(onClickListener);
        club.setOnClickListener(onClickListener);
        picture.setOnClickListener(onClickListener);
        my.setOnClickListener(onClickListener);
    }

    private void initFragment() {
        listfragment = new ArrayList<Fragment>();
        listfragment.add(new HomeFragment());
        listfragment.add(new TrainListFragment());
        listfragment.add(new ClubListFragment());
        listfragment.add(new PictureListFragment());
        listfragment.add(new MyFragment());

        fpadapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return listfragment.get(position);
            }

            @Override
            public int getCount() {
                return listfragment.size();
            }
        };
        viewPager.setAdapter(fpadapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                System.out.println("----" + String.valueOf(position));
                viewPager.setCurrentItem(position);
                resetImage();
                selectTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    private void resetImage(){
        image_home.setImageResource(R.drawable.ic_unhome);
        image_train.setImageResource(R.drawable.ic_untrain);
        image_club.setImageResource(R.drawable.ic_unclub);
        image_picture.setImageResource(R.drawable.ic_unpicture);
        image_my.setImageResource(R.drawable.ic_unmy);
    }

    private void selectTab(int i) {
        switch (i) {
            case 0:
                image_home.setImageResource(R.drawable.ic_home);

                break;
            case 1:
                image_train.setImageResource(R.drawable.ic_train);
                break;
            case 2:
                image_club.setImageResource(R.drawable.ic_club);
                break;
            case 3:
                image_picture.setImageResource(R.drawable.ic_picture);
                break;
            case 4:
                image_my.setImageResource(R.drawable.ic_my);
                break;
        }
        viewPager.setCurrentItem(i);
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                selectTab(0);
                break;
//            case R.id.settings_menu:
//                Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
//                startActivity(intent);
//                finish();
//                break;
        }
        return true;
    }


}
